<template>
  <el-dialog v-model="dialogTableVisible"
             title="专家评审"
             width="80%">
    <div>
      <div class="title-col">
        评分详情
      </div>
      <div>
        <el-descriptions  border>
          <el-descriptions-item label="项目名称">
            {{data.reviewObjectName}}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div>
        <el-table
            :data="data.lv1"
            style="width: 100%"
            border
        >
          <el-table-column prop="lv1name" label="一级指标" width="200"></el-table-column>
          <el-table-column label="二级指标及分值">
            <template #default="{ row }">
              <el-table
                  :data="row.lv2"
                  style="width: 100%"
                  border
                  size="small"
              >
                <el-table-column prop="lv2name" label="二级指标"></el-table-column>
                <el-table-column prop="lv2mark" label="满分"></el-table-column>
                <el-table-column prop="reviewMark" label="评审得分">
                  <template #default="{ row: subRow }">
                    <el-input v-model="subRow.reviewMark"
                              placeholder="请输入评审分数"
                              @input="validateScore(subRow)"
                               @focus="onlyInputNumber"
                              @blur="onlyInputNumber"
                    ></el-input>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="href">
        <div @click="applyFile">申请书预览</div>
        <div @click="planFile">评审指南预览</div>
      </div>
      <el-form :model="data" label-width="auto" >
        <div>
          <el-form-item label="评审意见">
            <el-input v-model="data.reviewInfo" type="textarea" />
          </el-form-item>
        </div>
        <div>
          <el-form-item label="评审意见">
            <el-radio-group v-model="data.reviewResult" >
              <el-radio value="pass">建议通过</el-radio>
              <el-radio value="noPass">不建议通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleSave">暂存</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
<!--        <el-button type="primary" @click="cs">重置</el-button>-->
        <el-button @click="dialogTableVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {reviewdetails,sumbitreviewdetails,resettest,download} from  './../../api/specialist/specialistApi'
import {ElMessage} from "element-plus";

const data = ref(
    // {
    //   applyFile: "93977c3184005ed58084ba70e4749064,f0603b01978b35a6a122e615d9191bae",
    //   expertReviewId: "专家评审测试_1",
    //   "lv1": [
    //     {
    //       "standardId": "REVIEW_STANDARD_ID_1",
    //       "lv1name": "内容质量",
    //       "orderId": 0,
    //       "lv2": [
    //         {
    //           "lv2name": "准确性",
    //           "lv2mark": "20",
    //           "reviewMark": ""
    //         },
    //         {
    //           "lv2name": "深度与完整性",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         },
    //         {
    //           "lv2name": "清晰度",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         }
    //       ]
    //     },
    //     {
    //       "standardId": "REVIEW_STANDARD_ID_2",
    //       "lv1name": "创新性",
    //       "orderId": 1,
    //       "lv2": [
    //         {
    //           "lv2name": "独特性",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         },
    //         {
    //           "lv2name": "前瞻性",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         }
    //       ]
    //     },
    //     {
    //       "standardId": "REVIEW_STANDARD_ID_3",
    //       "lv1name": "呈现方式",
    //       "orderId": 2,
    //       "lv2": [
    //         {
    //           "lv2name": "视觉效果",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         },
    //         {
    //           "lv2name": "语言表达",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         }
    //       ]
    //     },
    //     {
    //       "standardId": "REVIEW_STANDARD_ID_4",
    //       "lv1name": "影响力",
    //       "orderId": 3,
    //       "lv2": [
    //         {
    //           "lv2name": "受众共鸣",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         },
    //         {
    //           "lv2name": "传播潜力",
    //           "lv2mark": "10",
    //           "reviewMark": ""
    //         }
    //       ]
    //     }
    //   ],
    //   planFile: null,
    //   reviewInfo: null,
    //   reviewObjectName: "INFO_1_POSTMAN 白雪book1",
    //   reviewResult: null,
    // }
)
const csid = ref('')

const dialogTableVisible = ref(false)



const open = (row)=>{
  csid.value = row.expertReviewId
  reviewdetails({
    expertReviewId: row.expertReviewId
  }).then((res)=>{
    if(res.code = 200){
      data.value = res.data
      dialogTableVisible.value = true
    }
  })
}
const validateScore = (subRow) => {
  // 确保输入只包含数字
  if (typeof subRow.reviewMark === 'string') {
    subRow.reviewMark = subRow.reviewMark.replace(/[^\d]/g, '');
  }

  const maxScore = parseInt(subRow.lv2mark);
  const currentScore = parseInt(subRow.reviewMark);

  console.log('maxScore:', maxScore);
  console.log('currentScore:', currentScore);

  // 验证分数不超过最大值
  if (!isNaN(currentScore) && currentScore > maxScore) {
    subRow.reviewMark = maxScore.toString();
  }
};
// 新增的方法，用于过滤非数字字符
const onlyInputNumber = (e) => {
  e.target.value = e.target.value.replace(/[^\d]/g, '');
}

//测试重置
const cs = function (){
  resettest({
    expertReviewId:csid.value
  })
      .then((res)=>{
        if(res.code == 200){
          // alert('123')
        }
      })
}
const emit = defineEmits(['Getlist'])
//暂存
const handleSave = function (){
  console.log(data.value)
  sumbitreviewdetails({
    ...data.value,
    reviewStatus:'notSubmit'
  })
      .then((res)=>{
        if(res.code == 200){
          ElMessage({
            type: 'success',
            message: res.msg
          });
          emit('Getlist')
          dialogTableVisible.value = false

        }else {
          ElMessage({
            type: 'info',
            message: res.msg
          });
        }
      })
}
//提交
const submit = function (){
  console.log(data.value)
  sumbitreviewdetails({
    ...data.value,
    reviewStatus:'submit'
  })
      .then((res)=>{
        if(res.code == 200){
          ElMessage({
            type: 'success',
            message: res.msg
          });
          dialogTableVisible.value = false
          emit('Getlist')
        }else {
          ElMessage({
            type: 'info',
            message: res.msg
          });
        }
      })
}
//下载
import axiosInstance from './../../utils/request';
const baseURL = axiosInstance.defaults.baseURL;
//申请书预览
const applyFile = ()=>{
  //console.log(data.value.applyFile)
  if(data.value.applyFile){
    let fileId = data.value.applyFile.split(',')
    console.log(fileId)
    const downloadUrl = baseURL+`/commonFile/download?fileId=`+fileId[0];
    console.log(downloadUrl)
    window.open(downloadUrl, '_blank');
    // download({
    //   //'fileId':fileId[0]
    //   'fileId':'94d23233879a07db95d699a9e93f9242'
    // })
    //     .then((res)=>{
    //
    //     })
  }else {
    ElMessage({
      type: 'info',
      message: '没有文件'
    });
  }
}

//评审指南预览
const planFile = ()=>{
  if(data.value.planFile){
    let fileId = data.value.planFile.split(',')
    const downloadUrl = baseURL+`/commonFile/download?fileId=`+fileId[0];
    window.open(downloadUrl, '_blank');
  }else {
    ElMessage({
      type: 'info',
      message: '没有文件'
    });
  }
}

defineExpose({
  open
})
</script>

<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 10px;
}
.href{
  width: 100%;
  display: flex;
  justify-content: flex-end;
  color:#23527c;
  margin: 20px 0;
}
.href > div{
  margin: 0 10px;
}
.href > div:hover{
  color:#23527c;
  cursor: pointer;
  text-decoration: underline;
}
.title-col {
  color: #337AB7;
  font-weight: bolder;
  margin: 10px 0;
}
</style>